package com.coolweather.news_app;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
//import android.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerlayout;
    private String TAG = "MainActivity";

    private List<Category> newsTypeList = new ArrayList<>();
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CategoryAdapter categoryAdapter;

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.main,menu);
//        return true;
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar)findViewById(R.id.toolbar);

        drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout_drawer);
        
        recyclerView = findViewById(R.id.news_category_recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        categoryAdapter = new CategoryAdapter(newsTypeList);
        recyclerView.setAdapter(categoryAdapter);
        initNewsCategory();

        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
                this, drawerlayout, toolbar,
                R.string.drawer_open, R.string.drawer_close);
        //同步drawerToggle按钮与侧滑菜单的打开（关闭）状态
        drawerToggle.syncState();

        drawerlayout.setDrawerListener(new DrawerLayout.DrawerListener() {

            @Override
            public void onDrawerStateChanged(int newState) {
                // 打开关闭状态切换时调用

            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                // 滑动的过程中执行 slideOffset：0~1
                View content = drawerlayout.getChildAt(0);
                View menu = drawerView;
                float scale = 1-slideOffset;//1~0
                float leftScale = (float) (1-0.3*scale);
                float rightScale = (float) (0.7f+0.3*scale);//0.7~1
                menu.setScaleX(leftScale);//1~0.7
                menu.setScaleY(leftScale);//1~0.7

                content.setScaleX(rightScale);
                content.setScaleY(rightScale);
                content.setTranslationX(menu.getMeasuredWidth()*slideOffset);//0~width
                Log.i(TAG,"slideOffset="+slideOffset+",leftScale="+leftScale+",rightScale="+rightScale);

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // 打开了菜单

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                // 关闭了菜单

            }
        });


    }

    private void initNewsCategory() {
        Category sports = new Category("SportsNews");
        newsTypeList.add(sports);

        Category financial = new Category("FinancialNews");
        newsTypeList.add(financial);

    }
}